package gui;

import engine.Node;
import engine.Time;
import java.awt.GridLayout;
import java.util.ArrayList;

/**
 * A panel that stores and manages TimeSetters.
 * @author Johnny
 */
public class TimeTable extends javax.swing.JPanel {

    /** Creates new form TimeTable */
    public TimeTable() {
        initComponents();
        timeSetters = new TimeSetter[0];
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    private TimeSetter timeSetters[];

    /**
     * Clears the gui from the schedules.
     */
    public void clearSchedules() {
        this.removeAll();
        timeSetters = new TimeSetter[0];
        this.revalidate();
        this.repaint();
    }

    /**
     * Updates the gui according to the schedules.
     * @param nodes the list of course points.
     * @param times the list of times.
     */
    public void setSchedules(ArrayList<Node> nodes, ArrayList<Time> times) {
        clearSchedules();
        int n = times.size();
        this.setLayout(new GridLayout(n, 1));
        timeSetters = new TimeSetter[n];
        for (int i = 0; i < n; i++) {
            timeSetters[i] = new TimeSetter();
            timeSetters[i].setTime(times.get(i));
            timeSetters[i].setLabel(nodes.get(i).getName());
            this.add(timeSetters[i]);
        }
        this.revalidate();
    }

    /**
     * Returns the list of times from the table.
     * @return the list of times.
     */
    public ArrayList<Time> getSchedules() {
        ArrayList<Time> times = new ArrayList<Time>(timeSetters.length);
        for (TimeSetter ts : timeSetters) {
            times.add(ts.getTime());
        }
        return times;
    }
}
